//
//  OKSickComplicationController.m
//  ZLFastDoctor
//
//  Created by answer on 16/5/22.
//  Copyright © 2016年 LS. All rights reserved.
//

#import "OKSickComplicationController.h"
#import "OKSickModel.h"
#import "OKSickComplicationModel.h"
#import <MJRefresh/MJRefresh.h>


static NSString *OKSickComplicationControllerReusIdentifier = @"OKSickComplicationControllerReusIdentifier";

@interface OKSickComplicationController () <UITableViewDataSource, UITableViewDelegate>
@property (strong, nonatomic) NSArray *data;
@property (strong, nonatomic) NSMutableArray *cellArray;
@property (strong, nonatomic) NSMutableArray *callBackArray;
@end

@implementation OKSickComplicationController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self reloData];
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:OKSickComplicationControllerReusIdentifier];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"确定" style:UIBarButtonItemStylePlain target:self action:@selector(sure)];
    
    // 设置回调（一旦进入刷新状态，就调用target的action，也就是调用self的loadNewData方法）
    MJRefreshGifHeader *header = [MJRefreshGifHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadNewData)];
    // 设置普通状态的动画图片
    UIImage *idleImages = [UIImage imageNamed:@"v2_pullRefresh1"];
    UIImage *pullingImages = [UIImage imageNamed:@"v2_pullRefresh2"];
    NSArray *array = @[idleImages, pullingImages];
    
    [header setImages:array forState:MJRefreshStateIdle];
    // 设置即将刷新状态的动画图片（一松开就会刷新的状态）
    [header setImages:array forState:MJRefreshStatePulling];
    // 设置正在刷新状态的动画图片
    [header setImages:array forState:MJRefreshStateRefreshing];
    // 设置header
    self.tableView.mj_header = header;
}

- (void)loadNewData {
    [NSThread sleepForTimeInterval:1.0];
    [self.tableView.mj_header endRefreshing];
}
#pragma mark - 导航栏右侧确定按钮点击响应事件
- (void)sure {
//    NSMutableArray *array = [NSMutableArray array];
//    for (NSInteger i = 0; i < self.cellArray.count; i ++) {
//        UITableViewCell *cell = self.cellArray[i];
//        if (cell.accessoryType == UITableViewCellAccessoryCheckmark) {
//            OKSickComplicationModel *model = self.data[i];
//            [array addObject:[NSString stringWithFormat:@"%@", model.complication_name]];
//        }
//    }
    
    // 回调数据
    if ([self.delegate respondsToSelector:@selector(sickComplicationControllerCallBack:)]) {
        [self.delegate sickComplicationControllerCallBack:self.callBackArray.copy];
    }
    [self.navigationController popViewControllerAnimated:YES];
    
}

- (void)reloData {
    [OKSickModel requestSickComplicationWithParams:self.params completion:^(NSArray *result) {
        self.data = result;
        [self.tableView reloadData];
    }];
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return self.data.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:OKSickComplicationControllerReusIdentifier forIndexPath:indexPath];
    [self.cellArray addObject:cell];
    
    OKSickComplicationModel *model = self.data[indexPath.row];
    cell.textLabel.text = model.complication_name;

    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = self.cellArray[indexPath.row];
    // 修改cell的样式
    (cell.accessoryType == UITableViewCellAccessoryCheckmark) ? (cell.accessoryType = UITableViewCellAccessoryNone) : (cell.accessoryType = UITableViewCellAccessoryCheckmark);
    
    OKSickComplicationModel *model = self.data[indexPath.row];
    if (cell.accessoryType == UITableViewCellAccessoryCheckmark) {
        [self.callBackArray addObject:model.complication_name];
    } else {
        [self.callBackArray removeObject:model.complication_name];
    }
    
}

- (NSMutableArray *)cellArray {
    if (_cellArray == nil) {
        _cellArray = [NSMutableArray array];
    }
    return _cellArray;
}

- (NSMutableArray *)callBackArray {
    if (_callBackArray == nil) {
        _callBackArray = [NSMutableArray array];
    }
    return _callBackArray;
}

@end







